package cp3120.view;

import cp3120.model.Credentials;
import cp3120.model.Purchase;
import cp3120.ucm.client.UCLoginUser;
import generic.swing.LoginDialogue;

/**
 * A Singleton that displays the Main User Interface.
 *
 * @author Adam Scarr
 * @since r7
 */
public class RealMainUI extends MainUI {
    private MainFrame frame;
    private LoginDialogue loginDialogue;
    private RegistrationUI registrationUI;
    private ConfirmDeleteUI confirmDeleteUI;
    private RoleListView roleEditor;
    private UserListView userEditor;
    private AirportList airportEditor;
    private ActiveUsersList activeUserViewer;
    private FlightSearchView flightSearchbox;
    private PurchaseView currentPurchaseView;

    /**
     * Class constructor that sets up all te internal components.
     */
    public RealMainUI() {
        frame = new MainFrame();
        frame.setVisible(true);
        setInstance(this);
    }

    protected MainUI createInstance() {
        return new RealMainUI();
    }

    /**
     * Simple getter
     * @return The main menu.
     */
    public MainMenu getMainMenu() {
        return frame.getMenu();
    }

    /**
     * Simple Getter
     * @return The login Dialogue.
     */
    public LoginDialogue getLoginDialogue() {
        if (loginDialogue == null) {
            loginDialogue = new LoginDialogue();
        }
        return loginDialogue;
    }

    public void loginUser(Credentials credentials) {
        loginDialogue.close();
        loginDialogue.setErrorVisible(false);
        new UCLoginUser(credentials).run();
    }

    /**
     * Simple Getter
     * @return The Registration UI.
     */
    public RegistrationUI getRegistrationUI() {
        if (registrationUI == null) {
            registrationUI = new RegistrationUI();
        }
        return registrationUI;
    }

    /**
     * Simple Getter
     * @return The Delete confirmation dialogue.
     */
    public ConfirmDeleteUI getConfirmDeleteUI() {
        if (confirmDeleteUI == null) {
            confirmDeleteUI = new ConfirmDeleteUI();
        }

        return confirmDeleteUI;
    }

    public RoleListView getRoleEditor() {
        if (roleEditor == null) {
            roleEditor = new RoleListView();
        }

        return roleEditor;
    }

    /**
     * Tells the MainUI to update itself.
     */
    public void update() {
        frame.update();
    }

    public ActiveUsersList getActiveUserViewer() {
        if (activeUserViewer == null) {
            activeUserViewer = new ActiveUsersList();
        }

        return activeUserViewer;
    }

    public AirportList getAirportEditor() {
        if (airportEditor == null) {
            airportEditor = new AirportList();
        }

        return airportEditor;
    }

    public UserListView getUserEditor() {
        if (userEditor == null) {
            userEditor = new UserListView();
        }

        return userEditor;
    }

    public MainFrame getFrame() {
        return frame;
    }

    public FlightSearchView getFlightSearchbox() {
        if (flightSearchbox == null) {
            flightSearchbox = new FlightSearchView();
        }
        return flightSearchbox;
    }

    public PurchaseView getCurrentPurchaseView() {
        if (currentPurchaseView == null) {
            currentPurchaseView = new PurchaseView(new Purchase());
        }

        return currentPurchaseView;
    }
}